package com.example.Controller;


import com.example.entity.Orderdetail;
import com.example.entity.Orders;
import com.example.entity.Users;
import com.example.service.OrderdetailService;
import com.example.service.OrdersService;
import com.example.service.UsersService;
import com.example.service.impl.OrderdetailServiceImpl;
import com.example.service.impl.OrdersServiceImpl;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

@Api(description = "这是订单控制器")
@Controller
@RequestMapping("/admin2")
public class OrdersCtrl {
    @Resource
    OrdersService ordersService;

    @Resource
    OrderdetailService orderdetailService;

    @ApiOperation(value = "获取所有订单",notes = "获取所有订单")
    @RequestMapping(value = "/orders",method = {RequestMethod.POST,RequestMethod.GET})
    public String getOrdersList(Model model,
                               @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize",required = false,defaultValue = "4") Integer pageSize){
        PageInfo<Orders> ordersListPage = ordersService.queryAll(pageNum,pageSize);
        model.addAttribute("ordersListPage",ordersListPage);

        return "ordersList";
    }

    @ApiOperation("根据ID删除订单")
    @GetMapping("/delOrders/{id}")
    public String delOrders( @PathVariable("id") int id, Model model) {
        int num = ordersService.deleteById(id);
        model.addAttribute("msg","删除成功！");
        return "forward:/admin2/orders";
    }

    @GetMapping("/orders/{id}")
    public String getOrdersById(@PathVariable("id") int id,Model model){
        Orders orders = ordersService.findById(id);
        model.addAttribute("orders",orders);

        return "ordersEdit";
    }

    @PostMapping("/editOrders")
    public String editOrders(Orders orders,Model model) {
        int num =  ordersService.update(orders);

        model.addAttribute("msg","添加成功");
        return "forward:/admin2/orders";
    }

    @GetMapping("/gotoAddOrders")
    public String gotoAddOrders(Model model){
        // 获取订单类别
        return "ordersAdd";
    }

    @PostMapping("/addOrders")
    public String addOrders(Orders orders){
        // 进行订单信息的保存
        int num = ordersService.insert(orders);
        return "forward:/admin2/orders";
    }

    @GetMapping("/test")
    public Orders test(){
        return  new Orders();
    }

    @GetMapping("/getXq/{id}")
    public String findByOid(@PathVariable("id") int id, Model model){
        Orderdetail orderdetail = orderdetailService.findByOid(id);
        model.addAttribute("orderdetail",orderdetail);
        return "xiangqing";
    }


}
